using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;

namespace PShop
{
    public partial class Form_productInsert : Form
    {
        OleDbConnection con = null;
        OleDbCommand cmd = null;
        OleDbDataReader reader;
        PShop.PShopDataSetTableAdapters.productTableAdapter pta = new PShop.PShopDataSetTableAdapters.productTableAdapter();
        
        

        public Form_productInsert()
        {
            InitializeComponent();
            
        
        }

        

        

        

        
        

        private void button_insert_Click(object sender, EventArgs e)
        {
            if (radioButton_newProduct.Checked)
            {
                con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/PShop.mdb");
                con.Open();

                cmd = new OleDbCommand("select count(*) from product where product_ID = '" + textBox_productID.Text + "'", con);

                panel_insertProductTextBoxes.Enabled = false;
                reader = cmd.ExecuteReader();
                reader.Read();

                if (Int32.Parse(reader.GetValue(0).ToString()) == 0)
                {
                    
                    cmd = new OleDbCommand("insert into product values('" + textBox_productID.Text + "','" + textBox_productName.Text + "','" + textBox_description.Text + "'," + double.Parse(maskedTextBoxPurchasePrice.Text) + "," + double.Parse(maskedTextBoxSellingPrice.Text) + "," + double.Parse(maskedTextBoxReorderLevel.Text) + "," + double.Parse(maskedTextBox_productStock.Text) + ",#" + System.DateTime.Today + "#)", con);
                    
                    cmd.ExecuteNonQuery();


                }

                else
                {
                    MessageBox.Show("Same product ID exists");
                }
                panel_insertProductTextBoxes.Enabled = true;
                con.Close();
            }

            else
            {
                try
                {
                    //pShopDataSet.product.FindByproduct_ID(comboBox_productList.Text);

                    pta.UpdateProduct(textBox_productName.Text, textBox_description.Text, int.Parse(maskedTextBoxPurchasePrice.Text), int.Parse(maskedTextBoxSellingPrice.Text), int.Parse(maskedTextBoxReorderLevel.Text), int.Parse(maskedTextBox_productStock.Text), comboBox_productList.Text);
                    pShopDataSet.product.AcceptChanges();
                    
                    pShopDataSet.product.WriteXml("c:/out.xml");
                    pta.Update(pShopDataSet.product);
                    
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.StackTrace);
                }
                
                MessageBox.Show("Update Success full");
            }

                    
                   
            
            
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form_sales().Show();
        }

        private void radioButton_editProduct_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_productInfo.Enabled = !radioButton_editProduct.Checked;
            comboBox_productList.Visible = radioButton_editProduct.Checked;
            if (radioButton_editProduct.Checked)
                button_insert.Text = "Edit Product";
            else
                button_insert.Text = "Insert Product";
            
        }

        private void Form_productInsert_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'pShopDataSet.product' table. You can move, or remove it, as needed.
            this.productTableAdapter.Fill(this.pShopDataSet.product);
            radioButton_editProduct.Checked = true;

        }

        private void getAllProductIDToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                //this.productTableAdapter.GetAllProductID(this.pShopDataSet.product);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void comboBox_productList_SelectedIndexChanged(object sender, EventArgs e)
        {
            PShop.PShopDataSet.productDataTable pdt = pta.GetProductInfo(comboBox_productList.SelectedValue.ToString());
            if (pdt.Rows.Count > 0) groupBox_productInfo.Enabled = true;
            textBox_productName.Text = pdt.Rows[0]["product_name"].ToString();
            textBox_description.Text = pdt.Rows[0]["product_desc"].ToString();
            maskedTextBoxPurchasePrice.Text = pdt.Rows[0]["product_purchase_price"].ToString();
            maskedTextBoxSellingPrice.Text = pdt.Rows[0]["product_salling_price"].ToString();
            maskedTextBoxReorderLevel.Text = pdt.Rows[0]["product_reorder_level"].ToString();
            maskedTextBox_productStock.Text = pdt.Rows[0]["product_stock"].ToString();
            
        }

        
    }
}